package com.uenpay.im.ui.old.activity.wallet;

import com.qiyunxin.android.http.listener.OnHttpRequestTextListener;
import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.qiyunxin.android.http.net.HttpRequestAsyncTaskQueue;
import com.qiyunxin.android.http.net.HttpRequestType;
import com.qiyunxin.android.http.net.HttpTextAsyncTask;
import com.uenpay.im.IMApplication;
import com.uenpay.im.invokeitems.HttpRequestHandle;

import java.util.ArrayList;

/**
 * @author SL
 * @version 1.0
 * @copyRight http://www.qiyunxin.com
 * @E-mail:
 * @date 创建时间：2016-5-6 下午8:47:05
 * @function 钱包相关
 */
public class WalletHandle {

    public interface IGetBalanceResultListener {
        void onGetBalanceResult(String balance, int status,
                                int haspaypassword);
    }

    ;

    public static void getBalance(
            final IGetBalanceResultListener mBalanceResultListener) {
        BalanceRedInvokItem item = new BalanceRedInvokItem();
        HttpRequestAsyncTaskQueue
                .getInstance()
                .AddTask(
                        new HttpTextAsyncTask(item)
                                .SetRequestType(HttpRequestType.SERVER_ONLY),
                        new OnHttpRequestTextListener() {

                            @Override
                            public void OnFail(boolean isCache, String reason) {
                                IMApplication.showToast(reason);
                            }

                            @Override
                            public void OnProgress(long total_bytes,
                                                   long downloaded_bytes) {
                            }

                            @Override
                            public void OnSuccess(HttpInvokeItem item,
                                                  boolean isCache, String cacheFilePath) {
                                BalanceRedInvokItem.BalanceRedInvokItemResult result = ((BalanceRedInvokItem) item)
                                        .getOutput();
                                if (result != null) {
                                    mBalanceResultListener.onGetBalanceResult(
                                            result.balance, result.status,
                                            result._haspaypassword);
                                }
                            }
                        });

    }

    public interface IChargeListener {
        void onChargeResult(
                ChargeInvokItem.ChargeInvokItemResult mChargeInvokItemResult);
    }

    ;

    public static void charge(String amount,
                              final IChargeListener mChargeListener) {
        ChargeInvokItem item = new ChargeInvokItem(amount);
        HttpRequestAsyncTaskQueue
                .getInstance()
                .AddTask(
                        new HttpTextAsyncTask(item)
                                .SetRequestType(HttpRequestType.SERVER_ONLY),
                        new OnHttpRequestTextListener() {

                            @Override
                            public void OnFail(boolean isCache, String reason) {
                                IMApplication.showToast(reason);
                            }

                            @Override
                            public void OnProgress(long total_bytes,
                                                   long downloaded_bytes) {
                            }

                            @Override
                            public void OnSuccess(HttpInvokeItem item,
                                                  boolean isCache, String cacheFilePath) {
                                ChargeInvokItem.ChargeInvokItemResult result = ((ChargeInvokItem) item)
                                        .getOutput();
                                if (result != null) {
                                    mChargeListener.onChargeResult(result);
                                } else {
                                    mChargeListener.onChargeResult(null);
                                }
                            }
                        });

    }

    public interface IChargeByCardListener {
        void onChargeResult(
                ChargeByCardInvokItem.ChargeByCardInvokItemResult mChargeByCardInvokItemResult);
    }

    public static void chargeByCard(String amount, String memo, final IChargeByCardListener mChargeListener) {
        ChargeByCardInvokItem item = new ChargeByCardInvokItem(amount, memo);
        HttpRequestAsyncTaskQueue
                .getInstance()
                .AddTask(
                        new HttpTextAsyncTask(item)
                                .SetRequestType(HttpRequestType.SERVER_ONLY),
                        new OnHttpRequestTextListener() {

                            @Override
                            public void OnFail(boolean isCache, String reason) {
                                IMApplication.showToast(reason);
                            }

                            @Override
                            public void OnProgress(long total_bytes,
                                                   long downloaded_bytes) {
                            }

                            @Override
                            public void OnSuccess(HttpInvokeItem item,
                                                  boolean isCache, String cacheFilePath) {
                                ChargeByCardInvokItem.ChargeByCardInvokItemResult result = ((ChargeByCardInvokItem) item)
                                        .getOutput();
                                if (result != null) {
                                    mChargeListener.onChargeResult(result);
                                } else {
                                    mChargeListener.onChargeResult(null);
                                }
                            }
                        });

    }

    public interface ICashListener {
        void onCashResult(CashInvokItem.CashInvokItemResult mCashInvokItemResult);
    }

    ;

    /**
     * 提现
     *
     * @param amount
     */
    public static void cash(String amount, String paypassword,
                            final ICashListener mCashListener) {
        CashInvokItem item = new CashInvokItem(amount, paypassword);
        HttpRequestAsyncTaskQueue
                .getInstance()
                .AddTask(
                        new HttpTextAsyncTask(item)
                                .SetRequestType(HttpRequestType.SERVER_ONLY),
                        new OnHttpRequestTextListener() {

                            @Override
                            public void OnFail(boolean isCache, String reason) {
                                IMApplication.showToast(reason);
                            }

                            @Override
                            public void OnProgress(long total_bytes,
                                                   long downloaded_bytes) {
                            }

                            @Override
                            public void OnSuccess(HttpInvokeItem item,
                                                  boolean isCache, String cacheFilePath) {
                                CashInvokItem.CashInvokItemResult result = ((CashInvokItem) item)
                                        .getOutput();
                                if (result != null) {
                                    mCashListener.onCashResult(result);
                                } else {
                                    mCashListener.onCashResult(null);
                                }
                            }
                        });

    }

    public interface IGetPaymentsListResultListener {
        void onPaymentsResult(int status, String msg,
                              ArrayList<PaymentEntity> arraylist);
    }

    /**
     * 获取收支明细列表
     *
     * @param subject
     * @param page
     * @param mIGetPaymentsListResultListener
     */
    public static void getPaymentsList(String subject, int page,
                                       final IGetPaymentsListResultListener mIGetPaymentsListResultListener) {
        GetPaymentsInvokItem item = new GetPaymentsInvokItem(subject, page);
        HttpRequestAsyncTaskQueue
                .getInstance()
                .AddTask(
                        new HttpTextAsyncTask(item)
                                .SetRequestType(HttpRequestType.SERVER_FAIL_AND_CACHE),
                        new OnHttpRequestTextListener() {

                            @Override
                            public void OnFail(boolean isCache, String reason) {
                            }

                            @Override
                            public void OnProgress(long total_bytes,
                                                   long downloaded_bytes) {
                            }

                            @Override
                            public void OnSuccess(HttpInvokeItem item,
                                                  boolean isCache, String cacheFilePath) {
                                GetPaymentsInvokItem.GetPaymentsInvokItemResult result = ((GetPaymentsInvokItem) item)
                                        .getOutput();
                                if (result != null) {
                                    mIGetPaymentsListResultListener
                                            .onPaymentsResult(result.status,
                                                    result.msg,
                                                    result.arrayList);
                                } else {
                                    mIGetPaymentsListResultListener
                                            .onPaymentsResult(-1, "", null);
                                }
                            }
                        });
    }

    public interface IGetSubjectResultListener {
        void onSubjectResult(int status, String msg,
                             ArrayList<String> arrayList);
    }

    /**
     * 获取收支明细类型列表
     */
    public static void getSubjectList(
            final IGetSubjectResultListener mIGetSubjectResultListener) {
        GetSubjectListInvokItem item = new GetSubjectListInvokItem();
        HttpRequestHandle.onBaseRequest(item,
                HttpRequestType.SERVER_FAIL_AND_CACHE,
                new HttpRequestHandle.IRequestResultListener() {

                    @Override
                    public void onResult(Object resultObject, boolean isCache,
                                         String failReason) {
                        if (resultObject != null) {
                            GetSubjectListInvokItem.GetSubjectListInvokItemResult result = ((GetSubjectListInvokItem) resultObject)
                                    .getOutput();
                            if (result != null) {
                                mIGetSubjectResultListener.onSubjectResult(
                                        result.status, result.msg,
                                        result.arrayList);
                            } else {
                                mIGetSubjectResultListener.onSubjectResult(-1,
                                        failReason, null);
                            }
                        } else {
                            mIGetSubjectResultListener.onSubjectResult(-1,
                                    failReason, null);
                        }
                    }
                });
    }
}
